/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/03/15
 */
package vn.smartware.co.server.action;

import java.sql.Connection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import vn.smartware.co.framework.action.BaseAction;
import vn.smartware.co.framework.bean.LoginDTO;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.config.SystemConfig;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.form.ActionForm;
import vn.smartware.co.framework.form.LoginForm;
import vn.smartware.co.java.net.commons.Utils;
import vn.smartware.co.server.dao.factory.AbstractLoginDAO;
import vn.smartware.co.server.dao.factory.DaoFactory;

/**
 * LoginAction
 * @author thuctt
 */
public class LoginAction extends BaseAction {

	private static Log log = LogFactory.getLog(LoginAction.class);
	
	/**
	 * login action
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm login(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		LoginForm form = (LoginForm)aForm;
		AbstractLoginDAO dao = DaoFactory.createLoginDAO();
		Connection conn = getConnection();
		
		try {
			log.info("LoginAction start....");
			
			boolean flg = dao.chkUserNameExist(form, conn);
			
			if (!flg) {
				form.setLoginSuccess(false);
				
			}else{
				String lockYmd = dao.isAccountLocked(form, conn);
				
				if(!Utils.isNullEmpty(lockYmd)){
					form.setLockOutDate(lockYmd);
					form.setLoginSuccess(false);
					
				}else{
					String pass = form.getPassword();
					dao.getLoginInfo(form, conn);
					if (form.getPassword().equals(pass)){
						flg = true;
					}else{
						flg = false;
					}
					
					if (!flg){
						if (form.getLoginIndex() == -1){
							dao.createFirstLoginTime(form, conn);
							form.setLoginIndex(dao.getMaxLoginNo(conn));
						}else{
							int cnt = dao.countLoginTryNumber(form, conn);
							if (cnt < BaseConstant.MAX_LOGIN_NUMBER){
								cnt++;
								form.setLoginTryNumber(cnt);
								dao.increaseTryNumber(form, conn);
								if (cnt == BaseConstant.MAX_LOGIN_NUMBER){
									dao.lockAccount(form, conn);
								}
							}
						}
						form.setLoginSuccess(false);
						
					}else{
						form = dao.getUserInfo(form, conn);
						if (form.getLoginTryNumber() > 0){
							dao.updateLoginSucessfulStatus(form, conn);
							
						}else if (form.getLoginTryNumber() < 0){
							dao.createFirstLoginTime(form, conn);
							LoginDTO user = (LoginDTO)form.getUserDTO();
							int maxNo = dao.getMaxLoginNo(conn);
							form.setLoginIndex(maxNo);
							user.setLoginNo(maxNo);
							form.setUserDTO(user);
						}
						form.setLoginSuccess(true);
					}
				}
			}
			
			commit(conn);
			log.info("LoginAction end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * logout action
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm logoutAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		LoginForm form = (LoginForm)aForm;
		AbstractLoginDAO dao = DaoFactory.createLoginDAO();
		Connection conn = getConnection();
		
		try {
			log.info("LoginAction start....");
			
			LoginDTO user = (LoginDTO)form.getUserDTO();
			dao.setLogoutTimer(user, conn);
			
			commit(conn);
			log.info("LoginAction end.");
			
		} catch (ServerException e) {
			log.error("ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
	
	/**
	 * close action
	 * @param request
	 * @param response
	 * @param config
	 * @param aForm
	 * @return ActionForm
	 * @throws ServerException
	 */
	public ActionForm closeAction(HttpServletRequest request,
			HttpServletResponse response, SystemConfig config, ActionForm aForm)
			throws ServerException {

		LoginForm form = (LoginForm)aForm;
		AbstractLoginDAO dao = DaoFactory.createLoginDAO();
		Connection conn = getConnection();
		
		try {
			log.info("closeAction start....");
			
			int cnt = dao.countLoginTryNumber(form, conn);
			
			if (cnt > 0 && cnt < BaseConstant.MAX_LOGIN_NUMBER){
				dao.resetLoginStatus(form, conn);
				commit(conn);
			}
			
			log.info("closeAction end.");
			
		} catch (ServerException e) {
			log.error("LoginAction closeAction ServerException.", e);
			rollback(conn);
			throw e;
		} finally {
			closeConnection(conn);
		}
		return form;
	}
}
